<?php
/**
 * Controller.class.php
 * - Use controler of project
 * @author PHUNG VAN TAM
 * @since version - Aug 4, 2011
 * $Id: $
 */
require_once(LIB_DAO_DIR . "/DanhMuc.class.php");
require_once(LIB_DAO_DIR . "/SanphamCategory.class.php");
require_once(LIB_DAO_DIR . "/SanphamSCategory.class.php");
require_once(LIB_DAO_DIR . "/MSanpham.class.php");
require_once(LIB_DAO_DIR . "/GioHang.class.php");
require_once(LIB_DAO_DIR . "/KhoHang.class.php");
require_once(LIB_DAO_DIR . "/MNews.class.php");
require_once(LIB_DAO_DIR . "/Service.class.php");
require_once(LIB_DAO_DIR . "/MUser.class.php");
require_once(LIB_DAO_DIR . "/MHotro.class.php");
require_once(LIB_DAO_DIR . "/MQuangcao.class.php");

class Controller {
	// Default properties
    public $tpl_title;
    public $loginFlag = false;
    public $dataUser;
    /**
     * init
     * - Constructor
     */
    public function __construct() {
    	$this->dataUser = Session::Get("MLOGIN");
    	$this->gioHangYour = array();
        if (is_array($this->dataUser) && !empty($this->dataUser)) {
        	$this->loginFlag = true;
        	
        	$this->gioHangYour = $this->getGioHangYou($this->dataUser['id']);
        }
		$this->home = false;
		$this->giaCuoc = false;
		$this->dichVu = false;
		$this->khuyenMai = false;
		$this->tinTuc = false;
		$this->huongDan = false;
		$this->lienHe = false;  
        $this->listKhuyenMai = $this->getListNews('1');
        $this->listDichVu = $this->getListNameDichVu();
        $ojbDanhMuc = new DanhMuc();
        $this->listDanhMuc = $ojbDanhMuc->getAllByAttribute(array());
        $ojbCate = new SanphamCategory();
        $this->listCategory = $ojbCate->getAllByAttribute(array(), array('id', 'danhmuc_id', 'code', 'name'));
        $ojbCateSub = new SanphamSCategory();
        $this->listSubCategory = $ojbCateSub->getAllByAttribute(array(), array('id', 'danhmuc_id', 'iddm', 'code', 'name'));
        $ojbHotro = new MHotro();
        $this->listNickName = $ojbHotro->getAllByAttribute(array(), array('id', 'nickname', 'name'), array(0,3));
        $ojbQuangcao = new MQuangcao();
        $this->listImagequangcao = $ojbQuangcao->getAllByAttribute(array(), array('id', 'url', 'code'));
        // Cap nhat gio hang
        $ojbGioHang = new GioHang();
        $listSanphamA = $ojbGioHang->getThongTinGioHang();
    	if (count ($listSanphamA) > 0) {
        	foreach ($listSanphamA as $listSanpham) {
        		// Update table Gio Hang
        		$ojbGioHang->setAttribute('del', '1');
        		$ojbGioHang->update(array('id' => $listSanpham['id']));
        		$ojbGioHang->emptyAttribute();
        	}
        }
    }
    
    public function getMenuLeft() {
    	$ojbDanhMuc = new DanhMuc();
    	$ojbCate = new SanphamCategory();
    	$ojbCateSub = new SanphamSCategory();
    	$arrayVa = array();
    	
    	$danhMucA = $ojbDanhMuc->getAllByAttribute(array());
		if (count($danhMucA) > 0) {
			$i=0;
			foreach ($danhMucA as $danhmuc) {
				$danhmuc['value'] = '';
				$arrayVa[$i] = $danhmuc;
		    	$dataCateA = $ojbCate->getListAll($danhmuc['id']);
		    	if (count($dataCateA) > 0) {
		    		$j=0;
		    		foreach ($dataCateA as $dataC) {
		    			$dataC['value'] = '';
		    			$arrayVa[$i]['value'][$j] = $dataC;
		    			$dataSCate = $ojbCateSub->getListAll($danhmuc['id'], $dataC['id'], true);
		    			if (count ($dataSCate) > 0) {
		    				foreach ($dataSCate as $dataSub) {
		    					$arrayVa[$i]['value'][$j]['value'][] = $dataSub;
		    				}
		    			} else {
		    				$arrayVa[$i]['value'][$j]['value']['iddmc'] = $ojbCateSub->getIDDMC($danhmuc['id'], $dataC['id']);
		    			}
		    			$j++;
		    		}
		    	}
		    	$i++;
			}
		}//echo"<pre>";print_r($arrayVa);exit;
    	return $arrayVa;
    }
	
    public function getGioHangYou($idUser) {
    	$ojbGioHang = new GioHang();
    	return $ojbGioHang->getGioHangYour($idUser);
    }
    
    public function getListNews($idc) {
    	$ojbNewsKM = new MNews();
    	return $ojbNewsKM->getListFromNews($idc, false, array(0,6));
    }
    
    public function getListNameDichVu() {
    	$ojbService = new Service();
    	return $ojbService->getListTietDe();
    }
}
?>